﻿using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using WMSABL.Warehouse;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.Warehouse
{
    public partial class Warehouse_PartsLocationFrm : Form
    {
        private string strWhere = "";
        private int PD_LayoutNID = 0;
        private string PD_LayoutField = "";
        private string Area = "";
        private string Postion = "";
        DataSet ds = new DataSet();
        PublicDataSet bll_PublicDataSet = new PublicDataSet();
        private int SH = 0;
        private int SW = 0;
        public Warehouse_PartsLocationFrm()
        {
            InitializeComponent();
            #region
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            int H1 = 0;
            int H2 = 0;
            if (File.Exists("SytemConfig.xml"))
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load("SytemConfig.xml");
                foreach (XmlNode item in xmldoc.GetElementsByTagName("DOC"))
                {
                    foreach (XmlNode items in item.ChildNodes)
                    {
                        if (items.Name == "H1")
                            H1 = Convert.ToInt32(items.InnerText.Trim());
                        else if (items.Name == "H2")
                            H2 = Convert.ToInt32(items.InnerText.Trim());
                    }
                }
            }
            SH = Screen.PrimaryScreen.Bounds.Height;
            SW = Screen.PrimaryScreen.Bounds.Width;
            if (SH == 768 && SW == 1366)
                dataGridView1.RowTemplate.Height = H1;
            else if (SH == 1080 && SW == 1920)
                dataGridView1.RowTemplate.Height = H2;
            else if (SH == 1050 && SW == 1680)
                dataGridView1.RowTemplate.Height = H2;
            else if (SH > 1050)
                dataGridView1.RowTemplate.Height = H2;
            else if (SH == 768 && SW == 1024)
                dataGridView1.RowTemplate.Height = H1;
            else
                dataGridView1.RowTemplate.Height = H1;
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.  
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
            #endregion
        }
        private void Warehouse_PartsLocationFrm_Load(object sender, EventArgs e)
        {
            this.strWhere = "";
            this.pageControl1.PageCurrent = 1;
            this.pageControl1.PageSize = 500;
            this.pageControl1.Bind();
        }
        private int DBind()
        {
            ElectronicWMSA.Comm.Forms.MsControls.PageData pageData = new ElectronicWMSA.Comm.Forms.MsControls.PageData();
            pageData.TableName = "PD_Layout";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl1.PageCurrent;
            pageData.PageSize = this.pageControl1.PageSize;
            pageData.QueryCondition = strWhere;
            pageData.QueryFieldName = "*";

            this.pageControl1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl1.bindingNavigator.BindingSource = pageControl1.bindingSource;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = this.pageControl1.bindingSource;
            SystemVariable.DataGridViewStyle(dataGridView1);

            
                dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            if (SH > 768 && SW > 1366)
            {
                dataGridView1.RowsDefaultCellStyle.Font = new Font("微软雅黑", 8, FontStyle.Regular);
                dataGridView1.Rows[0].DefaultCellStyle.Font = new Font("微软雅黑", 10, FontStyle.Regular);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.Font = new Font("微软雅黑", 10, FontStyle.Regular);
            }
            else
            {
                dataGridView1.RowsDefaultCellStyle.Font = new Font("微软雅黑", 4, FontStyle.Regular);
                dataGridView1.Rows[0].DefaultCellStyle.Font = new Font("微软雅黑", 6, FontStyle.Regular);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.Font = new Font("微软雅黑", 6, FontStyle.Regular);
            }

            dataGridView1.Rows[0].DefaultCellStyle.BackColor = System.Drawing.Color.MediumSeaGreen;
            dataGridView1.Rows[dataGridView1.RowCount - 1].DefaultCellStyle.BackColor = System.Drawing.Color.MediumSeaGreen;
            return pageData.TotalCount;
        }
        Color Color_value = System.Drawing.Color.White;
        private int pageControlU_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBind();
        }
        /// <summary>
        /// 信息获取
        /// </summary>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            SelectRowindex = -1;
            if (e.RowIndex > -1 && e.ColumnIndex > 1)
            {
                SelectRowindex = e.RowIndex;
                PD_LayoutNID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["NID"].Value);
                PD_LayoutField = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();
                if (dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[e.ColumnIndex].Value != null)
                {
                    Area = dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[e.ColumnIndex].Value.ToString();
                    Postion = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                }
            }
        }

        #region  查询定位部品信息
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtProductName.Text.Trim() != "")
            {
                this.pageControl1.Bind();
                ds = new DataSet();
                ds = bll_PublicDataSet.GetDataSet("select A.Layout_NID,A.Layout_Field from PD_PostionToLayout as A left join T_Stock as B on A.PositionNO=B.Posiont1 where B.Product_Model='" + txtProductName.Text.Trim() + "' and PickList_NO is null ");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        for (int j = 0; j < dataGridView1.RowCount; j++)
                        {
                            if (dataGridView1.Rows[j].Cells["NID"].Value.ToString() == ds.Tables[0].Rows[i]["Layout_NID"].ToString())
                            {
                                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                                {
                                    if (dataGridView1.Columns[y].HeaderText == ds.Tables[0].Rows[i]["Layout_Field"].ToString())
                                    {
                                        this.dataGridView1.Rows[j].Cells[y].Style.BackColor = System.Drawing.Color.Red;
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                else
                {
                    MessageClass.warning("未找到信息，请核对！");
                }
            }
            else
            {
                MessageClass.warning("请填写成品名称！");
            }
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtProductName.Text = "";
            this.pageControl1.Bind();
        }
        #endregion

        private int SelectRowindex = -1;
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex != -1 && SelectRowindex != -1)
            {
                this.dataGridView1.Rows[SelectRowindex].DefaultCellStyle.BackColor = System.Drawing.Color.Gainsboro;
                ds = new DataSet();
                ds = bll_PublicDataSet.GetDataSet("SELECT a.Posiont1,a.Product_Model FROM T_Stock AS a LEFT JOIN PD_PostionToLayout AS b ON b.PositionNO=a.Posiont1 WHERE b.Layout_NID='" + PD_LayoutNID + "' AND b.Layout_Field='" + PD_LayoutField + "' AND a.PickList_NO IS NULL AND a.BK06!='0'  ");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Postion_LayoutDataFrm frm = new Postion_LayoutDataFrm(ds);
                    frm.ShowDialog();
                    this.dataGridView1.ClearSelection();
                }
                else
                {
                    MessageClass.warning("此区域无信息！");
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 布局反向
        /// </summary>
        private void checkBoxDirection_Click(object sender, EventArgs e)
        {
            if (checkBoxDirection.Checked)
            {
                #region 排序
                this.dataGridView1.Columns[0].DisplayIndex = 56;
                this.dataGridView1.Columns[1].DisplayIndex = 55;
                this.dataGridView1.Columns[2].DisplayIndex = 54;
                this.dataGridView1.Columns[3].DisplayIndex = 53;
                this.dataGridView1.Columns[4].DisplayIndex = 52;
                this.dataGridView1.Columns[5].DisplayIndex = 51;
                this.dataGridView1.Columns[6].DisplayIndex = 50;

                this.dataGridView1.Columns[7].DisplayIndex = 49;
                this.dataGridView1.Columns[8].DisplayIndex = 48;
                this.dataGridView1.Columns[9].DisplayIndex = 47;
                this.dataGridView1.Columns[10].DisplayIndex = 46;
                this.dataGridView1.Columns[11].DisplayIndex = 45;
                this.dataGridView1.Columns[12].DisplayIndex = 44;
                this.dataGridView1.Columns[13].DisplayIndex = 43;
                this.dataGridView1.Columns[14].DisplayIndex = 42;
                this.dataGridView1.Columns[15].DisplayIndex = 41;
                this.dataGridView1.Columns[16].DisplayIndex = 40;

                this.dataGridView1.Columns[17].DisplayIndex = 39;
                this.dataGridView1.Columns[18].DisplayIndex = 38;
                this.dataGridView1.Columns[19].DisplayIndex = 37;
                this.dataGridView1.Columns[20].DisplayIndex = 36;
                this.dataGridView1.Columns[21].DisplayIndex = 35;
                this.dataGridView1.Columns[22].DisplayIndex = 34;
                this.dataGridView1.Columns[23].DisplayIndex = 33;
                this.dataGridView1.Columns[24].DisplayIndex = 32;
                this.dataGridView1.Columns[25].DisplayIndex = 31;
                this.dataGridView1.Columns[26].DisplayIndex = 30;

                this.dataGridView1.Columns[27].DisplayIndex = 29;
                this.dataGridView1.Columns[28].DisplayIndex = 28;
                this.dataGridView1.Columns[29].DisplayIndex = 27;
                this.dataGridView1.Columns[30].DisplayIndex = 26;
                this.dataGridView1.Columns[31].DisplayIndex = 25;
                this.dataGridView1.Columns[32].DisplayIndex = 24;
                this.dataGridView1.Columns[33].DisplayIndex = 23;
                this.dataGridView1.Columns[34].DisplayIndex = 22;
                this.dataGridView1.Columns[35].DisplayIndex = 21;
                this.dataGridView1.Columns[36].DisplayIndex = 20;

                this.dataGridView1.Columns[37].DisplayIndex = 19;
                this.dataGridView1.Columns[38].DisplayIndex = 18;
                this.dataGridView1.Columns[39].DisplayIndex = 17;
                this.dataGridView1.Columns[40].DisplayIndex = 16;
                this.dataGridView1.Columns[41].DisplayIndex = 15;
                this.dataGridView1.Columns[42].DisplayIndex = 14;
                this.dataGridView1.Columns[43].DisplayIndex = 13;
                this.dataGridView1.Columns[44].DisplayIndex = 12;
                this.dataGridView1.Columns[45].DisplayIndex = 11;
                this.dataGridView1.Columns[46].DisplayIndex = 10;

                this.dataGridView1.Columns[47].DisplayIndex = 9;
                this.dataGridView1.Columns[48].DisplayIndex = 8;
                this.dataGridView1.Columns[49].DisplayIndex = 7;
                this.dataGridView1.Columns[50].DisplayIndex = 6;
                this.dataGridView1.Columns[51].DisplayIndex = 5;
                this.dataGridView1.Columns[52].DisplayIndex = 4;
                this.dataGridView1.Columns[53].DisplayIndex = 3;
                this.dataGridView1.Columns[54].DisplayIndex = 2;
                this.dataGridView1.Columns[55].DisplayIndex = 1;
                this.dataGridView1.Columns[56].DisplayIndex = 0;
                #endregion
            }
            else
            {
                #region 排序
                this.dataGridView1.Columns[0].DisplayIndex = 0;
                this.dataGridView1.Columns[1].DisplayIndex = 1;
                this.dataGridView1.Columns[2].DisplayIndex = 2;
                this.dataGridView1.Columns[3].DisplayIndex = 3;
                this.dataGridView1.Columns[4].DisplayIndex = 4;
                this.dataGridView1.Columns[5].DisplayIndex = 5;
                this.dataGridView1.Columns[6].DisplayIndex = 6;
                this.dataGridView1.Columns[7].DisplayIndex = 7;
                this.dataGridView1.Columns[8].DisplayIndex = 8;
                this.dataGridView1.Columns[9].DisplayIndex = 9;
                this.dataGridView1.Columns[10].DisplayIndex = 10;
                this.dataGridView1.Columns[11].DisplayIndex = 11;
                this.dataGridView1.Columns[12].DisplayIndex = 12;
                this.dataGridView1.Columns[13].DisplayIndex = 13;
                this.dataGridView1.Columns[14].DisplayIndex = 14;
                this.dataGridView1.Columns[15].DisplayIndex = 15;
                this.dataGridView1.Columns[16].DisplayIndex = 16;
                this.dataGridView1.Columns[17].DisplayIndex = 17;
                this.dataGridView1.Columns[18].DisplayIndex = 18;
                this.dataGridView1.Columns[19].DisplayIndex = 19;
                this.dataGridView1.Columns[20].DisplayIndex = 20;
                this.dataGridView1.Columns[21].DisplayIndex = 21;
                this.dataGridView1.Columns[22].DisplayIndex = 22;
                this.dataGridView1.Columns[23].DisplayIndex = 23;
                this.dataGridView1.Columns[24].DisplayIndex = 24;
                this.dataGridView1.Columns[25].DisplayIndex = 25;
                this.dataGridView1.Columns[26].DisplayIndex = 26;
                this.dataGridView1.Columns[27].DisplayIndex = 27;
                this.dataGridView1.Columns[28].DisplayIndex = 28;
                this.dataGridView1.Columns[29].DisplayIndex = 29;
                this.dataGridView1.Columns[30].DisplayIndex = 30;
                this.dataGridView1.Columns[31].DisplayIndex = 31;
                this.dataGridView1.Columns[32].DisplayIndex = 32;
                this.dataGridView1.Columns[33].DisplayIndex = 33;
                this.dataGridView1.Columns[34].DisplayIndex = 34;
                this.dataGridView1.Columns[35].DisplayIndex = 35;
                this.dataGridView1.Columns[36].DisplayIndex = 36;
                this.dataGridView1.Columns[37].DisplayIndex = 37;
                this.dataGridView1.Columns[38].DisplayIndex = 38;
                this.dataGridView1.Columns[39].DisplayIndex = 39;
                this.dataGridView1.Columns[40].DisplayIndex = 40;
                this.dataGridView1.Columns[41].DisplayIndex = 41;
                this.dataGridView1.Columns[42].DisplayIndex = 42;
                this.dataGridView1.Columns[43].DisplayIndex = 43;
                this.dataGridView1.Columns[44].DisplayIndex = 44;
                this.dataGridView1.Columns[45].DisplayIndex = 45;
                this.dataGridView1.Columns[46].DisplayIndex = 46;
                this.dataGridView1.Columns[47].DisplayIndex = 47;
                this.dataGridView1.Columns[48].DisplayIndex = 48;
                this.dataGridView1.Columns[49].DisplayIndex = 49;
                this.dataGridView1.Columns[50].DisplayIndex = 50;
                this.dataGridView1.Columns[51].DisplayIndex = 51;
                this.dataGridView1.Columns[52].DisplayIndex = 52;
                this.dataGridView1.Columns[53].DisplayIndex = 53;
                this.dataGridView1.Columns[54].DisplayIndex = 54;
                this.dataGridView1.Columns[55].DisplayIndex = 55;
                this.dataGridView1.Columns[56].DisplayIndex = 56;
                #endregion
            }
        }
    }
}
